
var cardMgr = require("./cardMgr");

const CardDecor = cardMgr.CardDecor;


class SGCardMgr extends cardMgr.CardMgr {
    // 豹子
    isLeopard(cards) {
        var firstValue;
        for (let i = 0; i < cards.length; i++) {
            let value = this.getCardValue(cards[i]);
            if(firstValue == null){
                firstValue = value;
            }
            else if(value != firstValue){
                return false;
            }
        }
        return true;
    }

    // 大三公
    isBigThreeGrand(cards) {
        if (!this.isLeopard(cards)) return false;
        var value = this.getCardValue(cards[0]);
        if (value < 11) return false;
        return true;
    }

    // 小三公
    isSmallThreeGrand(cards) {
        if (!this.isLeopard(cards)) return false;
        var value = this.getCardValue(cards[0]);
        if (value > 10) return false;
        return true;
    }

    // 混三公
    isMixThreeGrand(cards) {
        var cardValue, isMix = false;
        for (let i = 0; i < cards.length; ++i) {
            let value = this.getCardValue(cards[i]);
            if (value < 11) return false;
            if (cardValue == null) cardValue = value;
            else if (cardValue != value) isMix = true;
        }
        return isMix;
    }

    getMixCardPoint(cards) {
        var point = 0;
        for (let i = 0; i < cards.length; ++i) {
            let value = this.getCardValue(cards[i]);
            if (value < 10) point += value;
        }
        point %= 10;
        return point;
    }

    getMaxCardValue(cards) {
        var maxValue = 0;
        for (let i = 0; i < cards.length; ++i) {
            let value = this.getCardValue(cards[i]);
            if (value > maxValue) maxValue = value;
        }
        return maxValue;
    }

    getMaxCardDecor(cards) {
        var maxValue = 0;
        var maxDecor = 99;  // 花色类型越小，对应的值越大
        for (let i = 0; i < cards.length; ++i) {
            let decor = getCardDecor(cards[i]);
            let value = this.getCardValue(cards[i]);
            if (value > maxValue || (value == maxValue && decor < maxDecor)) {
                maxValue = value;
                maxDecor = decor;
            }
        }
        return maxDecor;
    }

    getDecorPoint(decor) {
        return (4 - decor);
    }

    getGrandNum(cards) {
        var grandNum = 0;
        for (let i = 0; i < cards.length; ++i) {
            if (this.getCardValue(cards[i]) > 10) {
                grandNum++;
            }
        }
        return grandNum;
    }

    //计算最终为牛几
    calculate(cards) {
        if (this.isBigThreeGrand(cards)) {
            return 300000 + this.getCardValue(cards[0]);
        }
        if (this.isSmallThreeGrand(cards)) {
            return 200000 + this.getCardValue(cards[0]);
        }
        if (this.isMixThreeGrand(cards)) {
            return 100000 + this.getMaxCardValue(cards) * 10 + this.getDecorPoint(this.getMaxCardDecor(cards));
        }
        return this.getMixCardPoint(cards) * 10000 + this.getGrandNum(cards) * 1000 + this.getMaxCardValue(cards) * 10 + this.getDecorPoint(this.getMaxCardDecor(cards));
    }
}

exports.SGCardMgr = SGCardMgr;